﻿using System;
using Abp.Dependency;
using Abp.Localization;
using Abp.MailKit;
using Abp.Modules;
using Abp.Reflection.Extensions;
using Abp.Timing;
using Abp.Zero;
using Abp.Zero.Configuration;
using pandx.Mulan.Authorization.Roles;
using pandx.Mulan.Authorization.Users;
using pandx.Mulan.Settings;
using pandx.Mulan.Localization;
using pandx.Mulan.MultiTenancy;
using pandx.Mulan.Net.Emailing;
using pandx.Mulan.Timing;
using Abp.Configuration.Startup;
using Abp.AutoMapper;
using Abp.Net.Mail;
using Abp.Net.Mail.Smtp;
using Castle.MicroKernel.Registration;
using MailKit.Security;
using pandx.Mulan.Features;
using pandx.Mulan.Notifications;

namespace pandx.Mulan
{
    [DependsOn(
        typeof(AbpZeroCoreModule),
        typeof(AbpAutoMapperModule),
        typeof(AbpMailKitModule))]
    public class MulanCoreModule : AbpModule
    {
        public override void PreInitialize()
        {
            Configuration.Auditing.IsEnabledForAnonymousUsers = true;
            
            Configuration.Modules.Zero().EntityTypes.Tenant = typeof(Tenant);
            Configuration.Modules.Zero().EntityTypes.Role = typeof(Role);
            Configuration.Modules.Zero().EntityTypes.User = typeof(User);
           
            AppRoleConfig.Configure(Configuration.Modules.Zero().RoleManagement);
            
            MulanLocalizationConfigurer.Configure(Configuration.Localization);
            
            Configuration.MultiTenancy.IsEnabled = CoreConsts.MultiTenancyEnabled;


            
            Configuration.Settings.Providers.Add<AppSettingProvider>();
            Configuration.Features.Providers.Add<AppFeatureProvider>();
            Configuration.Notifications.Providers.Add<AppNotificationProvider>();
            
            Configuration.Localization.Languages.Add(new LanguageInfo("fa", "فارسی", "famfamfam-flags ir"));
            
            Configuration.Modules.AbpMailKit().SecureSocketOption = SecureSocketOptions.Auto;
            Configuration.ReplaceService<IMailKitSmtpBuilder,MulanMailKitSmtpBuilder>(DependencyLifeStyle.Transient);
            Configuration.ReplaceService(typeof(IEmailSenderConfiguration), () =>
            {
                Configuration.IocManager.IocContainer.Register(
                    Component.For<IEmailSenderConfiguration,ISmtpEmailSenderConfiguration>()
                        .ImplementedBy<MulanSmtpEmailSenderConfiguration>()
                        .LifestyleTransient()
                );
            });
            
            Configuration.Caching.Configure("Temp", cache =>
            {
                cache.DefaultSlidingExpireTime = TimeSpan.FromMinutes(30);
            });
            
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(typeof(MulanCoreModule).GetAssembly());
        }

        public override void PostInitialize()
        {
            
            IocManager.Resolve<AppTimes>().StartupTime = Clock.Now;
        }
    }
}

